#include "KPF_DGramSession.h"	

#include "net_api.h"

KPF_DGramSession::KPF_DGramSession( const int8_t *pIP, const int16u_t iPort, const int16u_t iLocalPort )
{
	CSessionParam stUDPSessionParam;
	int8_t pLocalIP[64] = { 0x00, };
	
	net_get_local_ip( pLocalIP, sizeof(pLocalIP) );
	
	memset( &stUDPSessionParam, 0x00, sizeof(stUDPSessionParam) );
	memcpy( stUDPSessionParam.pIP, (char *)pIP, strlen((char *)pIP) + 1 );
	memcpy( stUDPSessionParam.pLocalIP, (char *)pLocalIP, strlen( (char *) pLocalIP) + 1 );
	stUDPSessionParam.iPort = iPort;
	stUDPSessionParam.iLocalPort = iLocalPort;
	
	pm_Session = create_session( SESSION_TYPE_DGRAM_CLIENT, &stUDPSessionParam );
	
	if ( pm_Session )
	{
		m_IP = (char *)pIP;
		m_Port = iPort;
		m_LocalPort = iLocalPort;
		
		if ( set_session_business( pm_Session, onSessionBusiness, this ) >= 0 )
		{
			if ( set_session_event( pm_Session, onSessionEvent ) >= 0 )
			{

			}
		}
	}
}
	
KPF_DGramSession::~KPF_DGramSession( void )
{

}

int32_t KPF_DGramSession::handle_input( const KPF_Socket *pSocket, const int8u_t *pData, const int32_t iDataLen )
{
	int32_t iRetCode = 0;
	
	if ( pData )
	{
		fwrite( pData, 1, iDataLen, stdout );
		fflush( stdout );
	}
	
	iRetCode = 0;
	
	return iRetCode;
}

int32_t KPF_DGramSession::handle_output( const KPF_Socket *pSocket, const int8u_t *pData, const int32_t iDataLen )
{
	int32_t iRetCode = -1;
	
	if ( pm_Session && pData && iDataLen > 0 )
		iRetCode = send_session_data( pm_Session, pData, iDataLen, NULL );	
	
	return iRetCode;
}

int32_t KPF_DGramSession::handle_event( const KPF_Event &eEvent )
{
	int32_t iRetCode = -1;
	
	return iRetCode;
}
